//
//  PriceDifferenceViewController.m
//  Housekeeping
//
//  Created by 辰领科技 on 16/5/18.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PriceDifferenceViewController.h"
#import "ChoseAddressViewController.h"
#import "PayTypeViewController.h"
#import "AddAddressViewController.h"

@interface PriceDifferenceViewController ()<BestScrollViewDelegate,BestScrollViewDatasource>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UILabel *textBackView;
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) IBOutlet UITextField *priceLabel;
@property (weak, nonatomic) IBOutlet UIView *addressView;


@property (weak, nonatomic) IBOutlet UILabel *reciverName;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UITextField *price;
@property(nonatomic,strong)NSDictionary * addressDic;

@property (nonatomic,strong)  BestScrollView *headerScroll;//滚动栏
@property (nonatomic,strong)  NSMutableArray  *imageArray;
@end

@implementation PriceDifferenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageArray = [NSMutableArray array];
    [self initViewLayout];
    [self queryDefaultMallShippingAddress];
    [self getGoodsDetail];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.addressView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 463), NO);
    
    _textBackView.layer.cornerRadius = 26.5*VerticalRatio();
    _textBackView.layer.borderWidth = 1;
    _textBackView.layer.borderColor = [UIColor colorWithWhite:0.941 alpha:1.000].CGColor;
    
    _priceLabel.layer.borderWidth = 1;
    _priceLabel.layer.borderColor = [UIColor colorWithWhite:0.855 alpha:1.000].CGColor;
    
    
    //广告栏
    _headerScroll=[[BestScrollView alloc]  initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 170), NO)];
    [self.scroView addSubview:_headerScroll];

}


- (IBAction)back:(id)sender {
    [self backBtn];
}


#pragma mark === 商品详情
- (void)getGoodsDetail{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"sectionId"] forKey:@"mstoAreaId"];
    [dic setObject:_goodsId forKey:@"goodsId"];
    [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryMallGoods withHttpType:@"POST" withTag:HK_HK_queryMallGoods];
    
}

#pragma mark  ==== 创建订单
- (IBAction)greatIndent:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    if ([Common dataIsNULL:_price.text]) {
        [CWHUDTips showTips:@"请输入价格"];
        return;
    }

    if (!_addressDic) {
        [CWHUDTips showLoadingTips:@"请添加收货地址信息"];
        return;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:_goodsId forKey:@"mgooId"];
    [dic setObject:@"2" forKey:@"mordSource"];
    [dic setObject:_addressDic[@"msadAreaId"] forKey:@"msadAreaId"];
    [dic setObject:_textView.text forKey:@"mordRemark"];
    [dic setObject:_addressDic[@"msadId"] forKey:@"mordAddressId"];
    [dic setObject:_price.text forKey:@"mordPrice"];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_saveMallOrder withHttpType:@"POST" withTag:HK_HK_saveMallOrder];
    
}



#pragma mark == 查询默认收货地址
- (void)queryDefaultMallShippingAddress{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryDefaultMallShippingAddress withHttpType:@"POST" withTag:HK_HK_queryDefaultMallShippingAddress];
    
}

#pragma mark   ===添加地址
- (IBAction)addAddressHandel:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
    ChoseAddressViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChoseAddress"];
    __weak __typeof(self) weakSelf = self;
    page.choseAddress = ^(NSDictionary * dic){
        weakSelf.addressDic = dic;
        _addressView.hidden = NO;
        _reciverName.text = dic[@"msadReceiverName"];
        _phone.text = dic[@"msadMobileNo"];
        _address.numberOfLines = 0;
        _address.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
        [_address sizeToFit];
    };
    [[Common getNavigation]pushViewController:page];
}
#pragma mark   ===修改地址
- (IBAction)changeAddress:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
    AddAddressViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AddAddress"];
    page.addressInfo = [_addressDic mutableCopy];
    page.endEditBlock = ^(NSString * delected){
        if ([delected isEqualToString:@"0"]) {
            [self getAddressInfo];
        }else{
            _addressView.hidden = YES;
        }
        
    };
    [[Common getNavigation]pushViewController:page];
    
}

#pragma mark  === 修改完地址后查询地址详情
- (void)getAddressInfo{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[NSString stringWithFormat:@"%@",_addressDic[@"msadId"]] forKey:@"msadId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryMallShippingAddressById withHttpType:@"POST" withTag:HK_HK_queryMallShippingAddressById];
}

#pragma mark   ====选择地址
- (IBAction)choseAddress:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
    ChoseAddressViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChoseAddress"];
    page.choseAddress = ^(NSDictionary * dic){
        _addressDic = dic;
        _addressView.hidden = NO;
        _reciverName.text = dic[@"msadReceiverName"];
        _phone.text = dic[@"msadMobileNo"];
        _address.numberOfLines = 0;
        _address.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
        [_address sizeToFit];
    };
    [[Common getNavigation]pushViewController:page];
    
}

#pragma  mark BestView Delegate Data=source=====================


- (NSInteger)numberOfPagesatView:(BestScrollView *)bsView
{
    
    return  self.imageArray.count;
    
}

- (UIView *)pageAtIndex:(NSInteger)index atView:(BestScrollView *)bsView
{
    
    if (self.imageArray.count>0) {
        
        UIImageView  *imageView=[[UIImageView alloc]  initWithFrame: flexibleFrame(CGRectMake(0, 0, 320, 170), NO)];
        [imageView  sd_setImageWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,_imageArray[index][@"mgimGoodsUrl"]]] placeholderImage:[UIImage imageNamed:@"integral2"]];
        return imageView;
    }
    return nil;
}


- (void)didClickPage:(BestScrollView *)csView atIndex:(NSInteger)index
{
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==HK_HK_queryDefaultMallShippingAddress || tag == HK_HK_queryMallShippingAddressById) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic = database[@"result"];
            _addressDic = dic;
            if (![dic isEqual:[NSNull null]]) {
                _addressView.hidden = NO;
                _reciverName.text = dic[@"msadReceiverName"];
                _phone.text = dic[@"msadMobileNo"];
                _address.numberOfLines = 0;
                _address.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
                [_address sizeToFit];
            }else{
                _addressView.hidden = YES;
            }
            
        }
    }else if (tag == HK_HK_saveMallOrder){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
            PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
            page.payOrderPrice = database[@"result"][@"mordPrice"];
            page.payOrderName = _goodsName;
            page.payId = database[@"result"][@"mordNo"];
            [[Common getNavigation]pushViewController:page];
        }
    }else if (tag==HK_HK_queryMallGoods) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic= database[@"result"];
            _imageArray = dic[@"listGoodsImage"];
            self.headerScroll.delegate=self;
            self.headerScroll.datasource=self;
            [self.headerScroll showImageArray:self.imageArray   withAnimation:YES];
            
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
